<template>
	<view class="get_free_box">
		<!-- <image class="bg" src="/static/image/invite-friend.jpg" mode="widthFix"></image> -->
		<text class="invite-num">已成功邀请 {{inviteContent.count}} 人</text>
		<view class="how-to-invite">
			<view class="how-to-invite-item">
				<image src="/static/image/invite01.png" mode="widthFix"></image>
				<view class="des">
					<text>点击按钮向好友</text>
					<text>发送邀请</text>
				</view>
			</view>
			<image class="arrow" src="/static/image/invite-arrow.png" mode="widthFix"></image>
			<view class="how-to-invite-item">
				<image src="/static/image/invite02.png" mode="widthFix"></image>
				<view class="des">
					<text>好友点击链接</text>
					<text>完成注册</text>
				</view>
			</view>
			<image class="arrow" src="/static/image/invite-arrow.png" mode="widthFix"></image>
			<view class="how-to-invite-item">
				<image src="/static/image/invite03.png" mode="widthFix"></image>
				<view class="des" style="width: 215rpx;">
					<text>好友开盒</text>
					<text>大于{{inviteContent.min_open}}星钻的福袋</text>
				</view>
			</view>
		</view>
		<view class="now-invite active" @click="shareNow" v-if="inviteContent.status==1">立即邀请</view>
		<view class="now-invite" v-else> 活动准备中，敬请期待</view>
		<ns-login ref="login"></ns-login>
		<!-- 分享弹窗 -->
		<view @touchmove.prevent.stop>
			<uni-popup ref="sharePopup" type="bottom" class="share-popup">
				<view>
					<view class="share-title">分享</view>
					<view class="share-content">
						<view class="share-box" @click="openPosterPopup">
							<button class="share-btn" :plain="true">
								<view class="iconfont iconpengyouquan"></view>
								<text>生成分享海报</text>
							</button>
						</view>
						<!-- #ifdef H5 -->
						<view class="share-box" @click="copyUrl">
							<button class="share-btn" :plain="true">
								<view class="iconfont iconfuzhilianjie"></view>
								<text>复制链接</text>
							</button>
						</view>
						<!-- #endif -->
						<view class="share-box" v-if="showShareFriend" @click="shareFriend">
							<button class="share-btn" :plain="true">
								<view class="iconfont iconiconfenxianggeihaoyou"></view>
								<text>分享给好友</text>
							</button>
						</view>
					</view>
					<view class="share-footer" @click="closeSharePopup"><text>取消分享</text></view>
				</view>
			</uni-popup>
		</view>
		<!-- 海报 -->
		<view @touchmove.prevent.stop>
			<uni-popup ref="posterPopup" type="bottom" class="poster-layer">
				<template v-if="poster != '-1'">
					<view>
						<view class="image-wrap">
							<image :src="$util.img(poster)" mode="widthFix" :show-menu-by-longpress="true" />
						</view>
						<!-- #ifdef APP-PLUS  -->
						<view class="share-code" @click="sharePoster()">分享二维码</view>
						<!-- <view class="save" @click="savePoster()">保存图片</view> -->
						<!-- #endif -->
						<!-- #ifdef H5 -->
						<!-- <view class="share-code">长按保存图片</view> -->
						<!-- #endif -->
					</view>
					<view class="close iconfont iconclose" @click="closePosterPopup()"></view>
				</template>
				<!-- <view v-else class="msg">{{ posterMsg }}</view> -->
			</uni-popup>
		</view>
		<view class="wx-tips" v-if="isWeiXin&&wxIsShare" @click="closeWXShare">
			<image class="img" src="/static/image/tips_img.png" mode="widthFix"></image>
		</view>
	</view>
</template>

<script>
	import config from "@/common/js/config.js";
	// #ifdef H5
	import {
		Weixin
	} from 'common/js/wx-jssdk.js';
	// #endif
	import uniCopy from '@/js_sdk/xb-copy/uni-copy.js'
	export default {
		data() {
			return {
				inviteContent: {
					status:1,
					count:0,
					min_open:0.0
				},
				inviteNum: 0,
				isWeiXin: false,
				poster: "-1", //海报
				// posterMsg: "", //海报错误信息
				wxIsShare: false,
			}
		},
		computed: {
			isLogin() {
				return this.$store.state.isLogin || false;
			},
			userInfo() {
				return this.$store.state.userInfo || {};
			},
			showShareFriend() {
				// #ifdef APP-PLUS
				return true;
				// #endif
				// if (this.isWeiXin) {
				// 	return true;
				// }
				return false;
			}
		},
		onLoad() {
			this.isWeiXin = this.$util.isWeiXin();
		},
		onShow() {
			if (this.isLogin) {
				this.$store.dispatch("getUserInfo");
			}
			this.getInvitenum();
		},
		methods: {
			// 获取邀请奖励活动邀请的人数
			async getInvitenum() {
				const res = await this.$api.sendRequest({
					url: "/api/fudai/invitenum",
					data: {
						invite: this.userInfo.member_id
					},
					async: false
				});
				if (res.code >= 0) {
					res.data.min_open = parseFloat(res.data.min_open);
					this.inviteContent = res.data;
				} else {
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
				}
			},
			// 复制链接
			copyUrl() {
				let text = this.inviteContent.url + '?code=' + this.inviteContent.code;;
				uniCopy({
					content: text,
					success: (res) => {
						this.closeSharePopup();
						uni.showToast({
							title: "分享链接已复制，请打开微信发送给好友",
							icon: 'none'
						})
					},
					error: (e) => {
						uni.showToast({
							title: e,
							icon: "none",
							duration: 3000,
						})
					}
				})
			},
			// 打开海报弹出层
			openPosterPopup() {
				this.getPoster();
				this.$refs.sharePopup.close();
				this.$refs.posterPopup.open();
			},
			// 关闭海报弹出层
			closePosterPopup() {
				this.$refs.posterPopup.close();
			},
			// 开启分享弹出层
			openSharePopup() {
				this.$refs.sharePopup.open();
			},
			// 关闭分享弹出层
			closeSharePopup() {
				this.$refs.sharePopup.close();
			},
			// 关闭微信分享
			closeWXShare() {
				this.wxIsShare = false;
			},
			//生成海报
			getPoster() {
				//活动海报信息
				let qrcode_param = {
					code: this.inviteContent.code
				};
				this.$api.sendRequest({
					url: "/memberrecommend/api/memberrecommend/poster",
					data: {
						page: this.inviteContent.url,
						qrcode_param: JSON.stringify(qrcode_param)
					},
					success: res => {
						if (res.code == 0) {
							this.poster = res.data.path + "?time=" + new Date().getTime();
							// console.log('this.poster', this.poster)
						} else {
							uni.showToast({
								title: res.message,
								icon: 'none'
							})
						}
					}
				});
			},
			sharePoster(){
				let url = this.$util.img(this.poster);
				console.log('url',url)
				uni.share({
				    provider: "weixin",
				    scene: "WXSceneSession",
				    type: 2,
				    imageUrl: url,
				    success: function (res) {
				        console.log("success:" + JSON.stringify(res));
				    },
				    fail: function (err) {
				        console.log("fail:" + JSON.stringify(err));
				    }
				});
			},
			savePoster() {
				let url = this.$util.img(this.poster);
				uni.downloadFile({
					url: url,
					success: (res) => {
						if (res.statusCode === 200) {
							uni.saveImageToPhotosAlbum({
								filePath: res.tempFilePath,
								success: () => {
									this.$util.showToast({
										title: "保存成功"
									});
								},
								fail: () => {
									this.$util.showToast({
										title: "保存失败，请稍后重试"
									});
								}
							});
						}
					},
					fail: (e) => {}
				});
			},
			// 分享好友
			shareFriend() {
				let shareUrl = this.inviteContent.url + '?code=' + this.inviteContent.code;
				let imageUrl = config.imgDomain + this.inviteContent.pic;
				// #ifdef APP-PLUS
				uni.share({
					provider: "weixin",
					scene: "WXSceneSession", //分享到聊天界面
					type: 0, //图文
					href: shareUrl,
					title: this.inviteContent.title,
					summary: this.inviteContent.content,
					imageUrl,
					success: (res) => {
						this.closeSharePopup();
						console.log("success:" + JSON.stringify(res));
					},
					fail: function(err) {
						console.log("fail:" + JSON.stringify(err));
					}
				});
				// #endif
				// // #ifdef H5
				// this.wxIsShare = true;
				// if (this.$util.isWeiXin()) {
				// 	// 微信公众号分享
				// 	this.$api.sendRequest({
				// 		url: '/wechat/api/wechat/share',
				// 		data: {
				// 			url: window.location.href
				// 		},
				// 		success: res => {
				// 			if (res.code == 0) {
				// 				var wxJS = new Weixin();
				// 				wxJS.init(res.data.jssdk_config);
				// 				let shareConfig = res.data.share_config,
				// 					url = this.$config.h5Domain + '/pages/goods/detail/detail?sku_id=' + this
				// 					.skuId;
				// 				wxJS.setShareData({
				// 					title: 'title',
				// 					desc: 'des',
				// 					link: url,
				// 					imgUrl: imageUrl
				// 				})
				// 			}
				// 		}
				// 	})
				// }
				// // #endif
			},
			// 立即邀请
			shareNow() {
				if (!this.isLogin) {
					this.$util.redirectTo("/otherpages/login/login/login", {
						back: '/magicpages/free_box/get_free_box'
					})
					return;
				}
				this.openSharePopup();
			}
		}
	}
</script>

<style lang="scss" scoped>
	.get_free_box {
		width: 100%;
		height: 100vh;
		background: url(../../static/image/invite-friend.jpg) no-repeat #FF3535;
		background-size: 100% auto;
		position: relative;

		.bg {
			position: absolute;
			width: 100%;
		}

		.invite-num {
			position: absolute;
			top: 250rpx;
			left: 50%;
			transform: translateX(-50%);
			font-size: 33rpx;
			font-weight: bold;
			color: #FFFFFF;
			width: 386rpx;
			height: 66rpx;
			line-height: 66rpx;
			background: url(../../static/image/invite-button.png) no-repeat;
			background-size: 100% auto;
			border-radius: 33rpx;
			text-align: center;
		}

		.how-to-invite {
			display: flex;
			position: absolute;
			top: 743rpx;
			left: 50%;
			transform: translateX(-50%);

			.how-to-invite-item {
				display: flex;
				flex-direction: column;
				align-items: center;

				&>image {
					width: 106rpx;
					height: 106rpx;
					flex-shrink: 0;
				}

				.des {
					margin-top: 10rpx;
					letter-spacing: 0;
					font-size: 21rpx;
					font-weight: 400;
					color: #444444;
					width: 168rpx;
					text-align: center;

					&>text {
						margin-top: 5rpx;
						display: block;
					}
				}
			}

			.arrow {
				width: 34rpx;
				height: 24rpx;
				margin: 46rpx 10rpx;
			}
		}

		.now-invite {
			width: 639rpx;
			height: 101rpx;
			background: linear-gradient(0deg, #B4B4B4, #F0F0F0);
			box-shadow: 4rpx 7rpx 14rpx 0px rgba(118, 10, 8, 0.4);
			// background: linear-gradient(0deg, #FFAA15, #FFFAE6);
			// box-shadow: 4rpx 7rpx 14rpx 0 rgba(118, 10, 8, 0.4);
			border-radius: 51rpx;
			// width: 669rpx;
			// height: 131rpx;
			// background: url(../../static/image/now-invite.png) no-repeat;
			// background-size: 100% auto;
			position: absolute;
			top: 1083rpx;
			// bottom: 19rpx;
			left: 50%;
			transform: translateX(-50%);
			line-height: 103rpx;
			font-size: 42rpx;
			font-family: Source Han Sans CN;
			font-weight: 500;
			color: #898989;
			text-align: center;

			&.active {
				background: linear-gradient(0deg, #FFAA15, #FFFAE6);
				box-shadow: 4rpx 7rpx 14rpx 0 rgba(118, 10, 8, 0.4);
				color: #D40202;
			}
		}
	}

	.wx-tips {
		width: 100vw;
		height: 100%;
		overflow: hidden;
		background-color: rgba(0, 0, 0, .7);
		position: absolute;
		top: 0;
		left: 0;
		z-index: 10001;

		.img {
			width: 600rpx;
			position: absolute;
			right: 20rpx;
			top: 20rpx;
		}
	}
</style>
<style lang="scss" scoped>
	.share-popup,
	.uni-popup__wrapper-box {
		.share-title {
			line-height: 60rpx;
			font-size: $font-size-toolbar;
			padding: 15rpx 0;
			text-align: center;
		}

		.share-content {
			display: flex;
			display: -webkit-flex;
			-webkit-flex-wrap: wrap;
			-moz-flex-wrap: wrap;
			-ms-flex-wrap: wrap;
			-o-flex-wrap: wrap;
			flex-wrap: wrap;
			padding: 15rpx;

			.share-box {
				flex: 1;
				text-align: center;

				.share-btn {
					margin: 0;
					padding: 0;
					border: none;
					line-height: 1;
					height: auto;

					text {
						margin-top: 20rpx;
						font-size: $font-size-tag;
						display: block;
						color: $color-title;
					}
				}

				.iconfont {
					font-size: 80rpx;
					line-height: initial;
				}

				.iconpengyouquan,
				.iconfuzhilianjie,
				.iconiconfenxianggeihaoyou {
					color: #07c160;
				}
			}
		}

		.share-footer {
			height: 90rpx;
			line-height: 90rpx;
			border-top: 2rpx #f5f5f5 solid;
			text-align: center;
			color: #666;
		}
	}

	.poster-layer {
		.generate-poster {
			padding: 40rpx 0;

			.iconfont {
				font-size: 80rpx;
				color: #07c160;
				line-height: initial;
			}

			>view {
				text-align: center;

				&:last-child {
					margin-top: 20rpx;
				}
			}
		}

		.image-wrap {
			width: 70%;
			margin: 30rpx auto 20rpx auto;
			box-shadow: 0 0 16px rgba(100, 100, 100, 0.3);
			line-height: 1;
			border-radius: 16rpx;
			overflow: hidden;

			image {
				width: 100%;
				height: 100%;
				height: 750rpx;
			}
		}

		.msg {
			padding: 40rpx;
		}
		.share-code{
			width: 697rpx;
			height: 94rpx;
			line-height: 94rpx;
			text-align: center;
			color: #FFFFFF;
			margin: 0 auto 10rpx;
			border-radius: 40rpx;
			background: linear-gradient(to right,#6185FF,#8B60FF);
		}
		.save {
			text-align: center;
			height: 80rpx;
			line-height: 80rpx;
		}

		.close {
			position: absolute;
			top: 0;
			right: 20rpx;
			width: 40rpx;
			height: 80rpx;
			font-size: 50rpx;
		}
	}
</style>
